//
//  PEMDailyExerciseModel.swift
//  RelationshipIntegration
//
//  Created by iOS-Czz on 2023/11/16.
//

import UIKit

class PEMDailyExerciseViewModel{
    var dataSource = [PEMDailyExerciseModel]()
    var duration = 0.0
    var distanceAll = 0.0
    var step = 0.0
    var engery = 0.0
}
extension PEMDailyExerciseViewModel{
    func loadData(_ type:Int,_ date:String,onSuccess: ((_ models: [PEMDailyExerciseModel]) ->Void)?){
        let dataHelper = PEMDailyExerciseModel.getUsingLKDBHelper()
        var sql = "select * from @t where date = '\(date)'"
        if type != -1 {
            sql = "select * from @t where type = '\(type)' and date = '\(date)'"
        }
        let modelArr = dataHelper.search(withSQL: sql, to: PEMDailyExerciseModel.self) as! [PEMDailyExerciseModel]
        self.dataSource = modelArr
        onSuccess?(self.dataSource)
    }
    
    func loadAllData(_ date:String,onSuccess: ((_ models: [PEMDailyExerciseModel]) ->Void)?){
        let dataHelper = PEMDailyExerciseModel.getUsingLKDBHelper()
        let sql = "select * from @t where date = '\(date)'"
        let modelArr = dataHelper.search(withSQL: sql, to: PEMDailyExerciseModel.self) as! [PEMDailyExerciseModel]
       
        duration = Double(modelArr.reduce(0) { $0 + $1.duration })
        distanceAll = Double(modelArr.reduce(0) { $0 + $1.distance })
        step = Double(modelArr.reduce(0) { $0 + $1.step })
        engery = Double(modelArr.reduce(0) { $0 + $1.engery })
//        self.dataSource = modelArr
        onSuccess?(self.dataSource)
    }
}

@objcMembers
class PEMDailyExerciseModel: PEMModel,Codable {
    var name = ""
    var type = 0
    var duration = 0.0
    var distance = 0.0
    var step = 0
    var engery = 0
    var date = ""
    var isDefault = true
    override class func getTableName() -> String {
        return  String(describing: PEMDailyExerciseModel.self)
    }
}
